package com.sun.vo;

import com.sun.pojo.UmsAdmin;
import com.sun.pojo.UmsMenu;
import com.sun.pojo.UmsRole;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class UmsAdminVo {
        // view object  视图值
        private String username;
        private String icon;
        private List<String> roles;
        private List<UmsMenu> menus;

        public static UmsAdminVo createInstance(UmsAdmin admin){
            UmsAdminVo vo = new UmsAdminVo();
            vo.setUsername(admin.getUsername());
            vo.setIcon(admin.getIcon());
            //一个用户拥有多个角色
            List<String> roles = new ArrayList<String>();
            for (UmsRole role : admin.getRoleList()) {
                roles.add(role.getName());
            }
            vo.setRoles(roles);

            //一个用户拥有很多菜单
            List<UmsMenu> menuList = new ArrayList<UmsMenu>();
            for (UmsRole role : admin.getRoleList()) {
                //一个role里面有很多菜单
                for (UmsMenu menu : role.getMenuList()) {
                    menuList.add(menu);
                }
            }
            vo.setMenus(menuList);
            return vo;
        }
}
